package com.lq.client.controller;

import com.lq.client.config.RestTempl;
import com.lq.client.vo.ResultVO;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.LoadBalancerClient;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class ClientController {

//    @Autowired
//    private RestTemplate restTemplate;

    @Autowired
    private ProductClinet productClinet;
//    @Autowired
//    private RestTempl restTempl;

//    @Autowired
//    private LoadBalancerClient loadBalancerClient;

    @HystrixCommand(fallbackMethod = "fallback")
    @GetMapping("/gett")
    public ResultVO getPro() {
        return productClinet.get();
//        ServiceInstance serviceInstance = loadBalancerClient.choose("TEST");
//        String url = String.format("http://%s:%s", serviceInstance.getHost(), serviceInstance.getPort()+"/product/list");
//        ResponseEntity<ResultVO> res = restTemplate.getForEntity("http://127.0.0.1:8080/product/list", ResultVO.class);
//        ResponseEntity<ResultVO> res = restTemplate.getForEntity(url, ResultVO.class);
//        ResponseEntity<ResultVO> res = restTemplate.getForEntity("http://PRO/list", ResultVO.class);
//        ResultVO forObject = restTemplate.getForObject("http://PRO/product/list", ResultVO.class);
//
//        return forObject;
    }

    private ResultVO fallback() {
        ResultVO resultVO = new ResultVO();
        resultVO.setCode(1);
        resultVO.setMsg("it is  busy");
        return resultVO;
    }
}
